# -*- coding: utf-8 -*-

import json
import ezdxf
from ezdxf.addons import odafc
from app.configs import tmp_path
from app.models.dao import exec_path
from app.tools.utils import arc as to_arc, ellipse as to_ellipse


def line(coordinates, features):
    result = {
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": coordinates
        },
        "properties": {
            "0_line_ID": None
        }
    }
    features.append(result)


def arc(coordinates, features):
    result = {
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": coordinates
        },
        "properties": {
            "0_arc_ID": None
        }
    }
    features.append(result)


def area(coordinates, features):
    result = {
        "type": "Feature",
        "geometry": {
            "type": "Polygon",
            "coordinates": coordinates
        },
        "properties": {
            "0_area_ID": None,
            "AREA": None
        }
    }
    features.append(result)


def ellipse(coordinates, features):
    result = {
        "type": "Feature",
        "geometry": {
            "type": "Polygon",
            "coordinates": coordinates
        },
        "properties": {
            "0_ellip_ID": None,
            "AREA": None
        }
    }
    features.append(result)


def text(coordinates, str, angle, size, features):
    result = {
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": coordinates
        },
        "properties": {
            "TEXTSTRING": str,
            "TEXT_ANGLE": angle,
            "TEXT_SIZE": size
        }
    }
    features.append(result)


def point(coordinates, features):
    result = {
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": coordinates
        },
        "properties": {
            "0_point_ID": None
        }
    }
    features.append(result)


def dwg2geojson(file):
    lines = {"type": "FeatureCollection", "features": []}
    arcs = {"type": "FeatureCollection", "features": []}
    areas = {"type": "FeatureCollection", "features": []}
    ellipses = {"type": "FeatureCollection", "features": []}
    texts = {"type": "FeatureCollection", "features": []}
    points = {"type": "FeatureCollection", "features": []}

    ezdxf.options.set('odafc-addon', 'win_exec_path', exec_path)
    doc = odafc.readfile(file)
    msp = doc.modelspace()
    for entity in msp:
        if entity.dxftype() == 'LWPOLYLINE':
            with entity.points('xy') as coordinates:
                if entity.closed:
                    coordinates.append(coordinates[0])
                    area(coordinates, areas["features"])
                else:
                    line(coordinates, lines["features"])
        elif entity.dxftype() == 'TEXT':
            coordinates = [entity.dxf.insert.x, entity.dxf.insert.y]
            str, angle, size = entity.dxf.text, entity.dxf.rotation, entity.dxf.height
            text(coordinates, str, angle, size, texts["features"])
        elif entity.dxftype() == 'INSERT':
            pass
        elif entity.dxftype() == 'LINE':
            coordinates = [
                [entity.dxf.start.x, entity.dxf.start.y],
                [entity.dxf.end.x, entity.dxf.end.y]
            ]
            line(coordinates, lines["features"])
        elif entity.dxftype() == 'MTEXT':
            pass
        elif entity.dxftype() == 'ELLIPSE':
            cx = entity.dxf.center.x
            cy = entity.dxf.center.y
            ux = entity.dxf.major_axis[0]
            uy = entity.dxf.major_axis[1]
            vx = entity.minor_axis[0]
            vy = entity.minor_axis[1]
            start_param = entity.dxf.start_param
            end_param = entity.dxf.end_param
            coordinates = to_ellipse(cx, cy, ux, uy, vx, vy, start_param, end_param)
            coordinates.append(coordinates[0])
            ellipse(coordinates, ellipses["features"])
            pass
        elif entity.dxftype() == 'HATCH':
            pass
        elif entity.dxftype() == 'CIRCLE':
            x = entity.dxf.center.x
            y = entity.dxf.center.y
            r = entity.dxf.radius
            coordinates = to_arc(x, y, r)
            arc(coordinates, arcs["features"])
        elif entity.dxftype() == 'ARC':
            x = entity.dxf.center.x
            y = entity.dxf.center.y
            r = entity.dxf.radius
            start_angle = entity.dxf.start_angle
            end_angle = entity.dxf.end_angle
            coordinates = to_arc(x, y, r, start_angle, end_angle)
            arc(coordinates, arcs["features"])
        elif entity.dxftype() == 'SPLINE':
            coordinates = [[item[0], item[1]] for item in entity.control_points]
            if entity.closed:
                coordinates.append(coordinates[0])
            line(coordinates, lines["features"])
        elif entity.dxftype() == 'POLYLINE' and entity.is_2d_polyline:
            pass
        elif entity.dxftype() == 'DIMENSION':
            pass
        elif entity.dxftype() == 'SOLID':
            pass
        elif entity.dxftype() == 'POINT':
            coordinates = [entity.dxf.location.x, entity.dxf.location.y]
            point(coordinates, points["features"])
        else:
            print(entity.dxftype())

    lines_file = tmp_path + "0_line.json"
    with open(lines_file, "w") as fw:
        fw.write(json.dumps(lines))

    arcs_file = tmp_path + "0_arc.json"
    with open(arcs_file, "w") as fw:
        fw.write(json.dumps(arcs))

    areas_file = tmp_path + "0_area.json"
    with open(areas_file, "w") as fw:
        fw.write(json.dumps(areas))

    ellipses_file = tmp_path + "0_ellipse.json"
    with open(ellipses_file, "w") as fw:
        fw.write(json.dumps(ellipses))

    texts_file = tmp_path + "0_text.json"
    with open(texts_file, "w") as fw:
        fw.write(json.dumps(texts))

    points_file = tmp_path + "0_point.json"
    with open(points_file, "w") as fw:
        fw.write(json.dumps(points))


if __name__ == '__main__':
    dwg2geojson(tmp_path + "塔矿采掘工程平面图.dwg")
